TOOL.Category    = "Conna's Tools"
TOOL.Name        = "#Barrier Tool"
TOOL.Command     = nil
TOOL.ConfigName  = ""

// ConVars

TOOL.ClientConVar["X"] = "1024"
TOOL.ClientConVar["Y"] = "1024"
TOOL.ClientConVar["Z"] = "1024"

// Register

cleanup.Register("barriers")

// ConVars

CreateConVar("sbox_maxbarriers", 10, FCVAR_NOTIFY)

// Client

if (CLIENT) then
	// Tool
	
	language.Add("Tool_barrier_name", "Barrier Tool")
	language.Add("Tool_barrier_desc", "Create a barrier which people cannot noclip through")
	language.Add("Tool_barrier_0", "Left click to spawn a Barrier")
	
	// Other
	
	language.Add("Undone_barrier", "Undone Barrier")
	language.Add("SBoxLimit_barriers", "You've hit the Barriers limit!")
	language.Add("Cleanup_barriers", "Barriers")
	language.Add("Cleaned_barriers", "Cleaned up all Barriers")
end

// Message

function TOOL:Message(Text)
	if (SERVER) then
		self:GetOwner():SendLua("GAMEMODE:AddNotify('"..Text.."', NOTIFY_GENERIC, 10)")
		self:GetOwner():SendLua("surface.PlaySound('ambient/water/drip"..math.random(1, 4)..".wav')")
	end
end

// Left click

function TOOL:LeftClick(Trace)
	if(CLIENT) then
		return true
	end
	
	// Locals
	
	local Player = self:GetOwner()
	local X = self:GetClientNumber("X")
	local Y = self:GetClientNumber("Y")
	local Z = self:GetClientNumber("Z")
	local Material = self:GetClientInfo("Material")
	
	// Keep it nice and whole
	
	X = math.Clamp(X, 1, 1024)
	Y = math.Clamp(Y, 1, 1024)
	Z = math.Clamp(Z, 1, 1024)
	
	// Spawn angles
	
	local Ang = Trace.HitNormal:Angle()
	
	Ang.pitch = Ang.pitch + 90
	
	// Spawn it

	local Barrier = CreateBarrier(Player, Ang, Trace.HitPos, X, Y, Z, Material)
	
	if not (Barrier) then return false end
	
	// Position
	
	local Minimum = Barrier:OBBMins()
	
	Barrier:SetPos(Trace.HitPos - Trace.HitNormal * Minimum.z)
	
	// Undo
	
	undo.Create("barrier")
		undo.AddEntity(Barrier)
		undo.SetPlayer(Player)
	undo.Finish()
	
	// Add count
	
	Player:AddCount("barriers", Barrier)
	
	Player:AddCleanup("barriers", Barrier)
	
	// Message
	
	self:Message("Barrier has been created!")
	
	return true, Barrier
end

// Make it

if (SERVER) then
	function CreateBarrier(Player, Ang, Pos, X, Y, Z, Material, Nocollide, Vel, Avel, Frozen)
		if (!Player:CheckLimit("barriers")) then return false end
		
		local Barrier = ents.Create("gmod_barrier")
		
		Barrier:SetDimensions(X, Y, Z)
		Barrier:SetModel("models/props/de_dust/du_barrier_64x64.mdl")
		Barrier:SetAngles(Ang)
		Barrier:SetPos(Pos)
		Barrier:Spawn()
		Barrier:Activate()
		
		// Other
		
		Barrier:UpdateEntityLabel()
		Barrier:SetPlayer(Player)
		
		// Merge
		
		local Table = {
			X = X,
			Y = Y,
			Z = Z
		}
		
		table.Merge(Barrier, Table)
		
		// Frozen and nocollide
		
		if (Frozen) then Barrier:GetPhysicsObject():EnableMotion(false) end
		
		if (Nocollide) then Barrier:GetPhysicsObject():EnableCollisions(false) end
		
		return Barrier
	end
	
	duplicator.RegisterEntityClass("gmod_barrier", CreateBarrier, "Ang", "Pos", "X", "Y", "Z", "Material", "Nocollide", "Vel", "Avel", "Frozen")
end

// Control panel

function TOOL.BuildCPanel(Panel)
	Panel:AddControl("Header", {Text = "#Tool_barrier_name", Description = "#Tool_barrier_desc"})
	
	Panel:AddControl("Slider", {Label = "X", Type = "Integer", Min = 1, Max = 1024, Command = "barrier_X"})
	
	Panel:AddControl("Slider", {Label = "Y", Type = "Integer", Min = 1, Max = 1024, Command = "barrier_Y"})
	
	Panel:AddControl("Slider", {Label = "Z", Type = "Integer", Min = 1, Max = 1024, Command = "barrier_Z"})
end